/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package telnet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketTimeoutException;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetInputListener;

/**
 *
 * @author modesto
 */
public class QuaggaTelnetClient {

    private TelnetClient tc;
    private String prompt;
    private String mode;
    public InputStream in;
    public PrintStream out;

    public QuaggaTelnetClient(String host, Integer port) throws IOException {
        tc = new TelnetClient();
        tc.connect(host, port);
        tc.setSoTimeout(5000);
        in = tc.getInputStream();
        out = new PrintStream(tc.getOutputStream());
        this.prompt = ">";
        this.mode = "normal";
    }
    
    public boolean enable(String password) throws IOException {
        StringBuffer buf;
        
        if (this.mode.equals("normal")) {
            println("enable");
            
            readUntil("Password:");
            
            println(password);

            try {
                readUntil("#");
                this.mode = "enable";
                this.prompt = "#";
                return true;
            } catch (SocketTimeoutException ex) {
                return false;
            }
            
        } else {
            return false;
        }   
    }
    
    public boolean leaveEnableMode() {
        if (this.mode.equals("enable")) {
            this.mode = "normal";
            return true;
        } else {
            return false;
        }
    }
    
    public StringBuffer sendCommand(String command) throws IOException {
        
        if (command == null)
            return null;
        
        println(command.trim());
        
        try {
            StringBuffer buf = readUntil(prompt);
            return buf;
        } catch (SocketTimeoutException ex) {
            return null;
        }
        
    }

    public boolean auth(String password) throws IOException {

        StringBuffer buf = this.readUntil("Password:");
        
        try {
            println(password);
            buf = this.readUntil(prompt);
            
            if (buf.toString().endsWith(prompt))
                return true;
        } catch (SocketTimeoutException ex) {
            return false;
        }
        
        return false;
    }
    
    public void disconnect() throws IOException {
        tc.disconnect();
    }

    private boolean emptyInputBuffer() throws IOException {
        return in.available() == 0;
    }

    public StringBuffer eatInput() throws IOException {
        StringBuffer buffer = new StringBuffer();

        while (!emptyInputBuffer()) {
            char c = (char) in.read();
            buffer.append(c);
        }

        return (buffer.length() > 0) ? buffer : null;
    }

    public StringBuffer readUntil(String pattern) throws IOException {
        StringBuffer buffer = new StringBuffer();
        Boolean found = false;

        char lastChar = pattern.charAt(pattern.length() - 1);
        char c = (char) in.read();

        while (!found) {
            buffer.append(c);
            if (c == lastChar && buffer.toString().endsWith(pattern)) {
                return buffer;
            }

            c = (char) in.read();
        }

        return null;
    }

    private void println(String value) {
        this.out.println(value);
        this.out.flush();
    }
    

}
